# -*- coding:utf-8 -*-

"""
外键，数据库+id：user_id
"""

from django.db import models
from django.db.models import Q

from Apps.common.custommodel import CustomModel, ModifyModel
from Apps.user.models import User

import logging

logger = logging.getLogger('django')


class Role(ModifyModel):
    roleName = models.CharField(max_length=64, verbose_name="角色名称")
    status = models.BooleanField(default=True, verbose_name="角色状态")
    defaultRole = models.BooleanField(default=False, verbose_name="是否默认角色")
    remark = models.TextField(verbose_name="备注", help_text="备注", null=True, blank=True)
    user = models.ManyToManyField(to=User, db_constraint=False, verbose_name="用户")

    class Meta:
        db_table = 'role'
        verbose_name = "角色表"
        ordering = ('-create_time',)


class Menu(ModifyModel):
    # MENU_TYPE_CHOICES = (
    #     ("0", "一级菜单"),
    #     ("1", "二级菜单"),
    #     ("2", "按钮"),
    # )
    parentId = models.CharField(max_length=50, verbose_name="上级菜单", default="", null=True, blank=True)
    menuType = models.CharField(max_length=8, verbose_name="菜单类型")
    icon = models.CharField(max_length=64, verbose_name="菜单图标", default="", null=True, blank=True)
    name = models.CharField(max_length=64, verbose_name="菜单名称", default="")
    orderNum = models.IntegerField(verbose_name="显示排序", default=0)
    web_path = models.CharField(max_length=128, verbose_name="前端路由地址", default="", null=True, blank=True,
                                help_text="菜单时（储存菜单跳转地址）；按钮时（存储按钮跳转的地址）")
    component_path = models.CharField(max_length=256, verbose_name="前端组件路径", default="", null=True, blank=True)
    interface_path = models.CharField(max_length=256, verbose_name="后端接口路径", default="", null=True, blank=True)
    interface_method = models.CharField(max_length=16, default='', verbose_name="接口请求方式")
    perms = models.CharField(max_length=256, verbose_name="权限标识,用来前端判定", default="", null=True, blank=True)
    status = models.BooleanField(default=True, verbose_name="菜单状态,是否启用")
    role = models.ManyToManyField(to=Role, db_constraint=False, verbose_name="角色")

    class Meta:
        db_table = 'menu'
        verbose_name = "菜单表"
        ordering = ('orderNum', 'create_time')

# class UserRole(CustomModel):
#     user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="user_role_user_id",
#                              db_constraint=False, verbose_name="用户ID")
#     role = models.ForeignKey(Role, on_delete=models.CASCADE, related_name="user_role_role_id",
#                              db_constraint=False, verbose_name="角色ID")
#
#     class Meta:
#         db_table = 'user_role'
#         verbose_name = "用户角色关系表"

# def __str__(self):
#     return self.user_id


# class RoleMenu(CustomModel):
#     role = models.ForeignKey(Role, on_delete=models.CASCADE, related_name="role_menu_role_id",
#                              db_constraint=False, verbose_name="角色ID")
#     menu = models.ForeignKey(Menu, on_delete=models.CASCADE, related_name="role_menu_menu_id",
#                              db_constraint=False, verbose_name="菜单ID")
#
#     class Meta:
#         db_table = 'role_menu'
#         verbose_name = "角色菜单关系表"
